<?php

require_once($_SERVER['DOCUMENT_ROOT'] . "/defaultconf.mex");
require_once($GLOBALS['BIB'] . "Objects/obj.mail.mex");
MYTCONN();
$GET = $GLOBALS['MYT']->db_array("SELECT tdm_ID, tdm_Domain, ref AS tdm_User, tdm_Corresponsal, tdm_Country, tdm_Cashier, tdm_DateValue, tdm_Date, tdm_Description, tdm_File, tdm_InvTotal, tdm_TFraSent, tdm_TFraPay, tdm_StartEnd, tdm_Service, tdm_Currency
			FROM transmitionsdomain 
				INNER JOIN systemoffice.main_users ON ref = tdm_User
			WHERE tdm_ID = {$_GET['ID']}");
// MakeFile
require_once($GLOBALS['BIB'] . "/FileTemplates/SpreadSheets/tReport01.mex");
$CONTENTS = "SELECT FraID AS Field1, CONCAT(FraOrigen0,'-',LPAD(FraReferenciaAg,6,0)) AS Field2, FraTasa AS Field3, FraCargo AS Field4, FraEnviado AS Field5, FraARecibir AS Field6, FraFechaIngreso AS Field7, CliNombre AS Field8, BnfNombre AS Field9
		FROM Facturas IGNORE INDEX (IdxCli)
		INNER JOIN clientes ON CliNumero = FraCliNumero
		INNER JOIN beneficiarios ON BnfNumero = FraBnfNumero
			WHERE FraFechaAlb = '{$GET[1]['tdm_Date']}' AND FraUsuarioAlb = {$GET[1]['tdm_User']}";

if ($GET[1]['tdm_Service'] == 1) {
    $rp_dep = 8;
} else {
    $rp_dep = 9;
}

$File = $GET[1]['tdm_Domain'] . str_replace(' ', '', str_replace('-', '', str_replace(':', '', $GET[1]['tdm_Date']))) . 'OTRNS.xls';
$SheetPad = $GLOBALS['DOC'] . 'OFTRANSM/';
SpreadSheet($SheetPad . $File, $CONTENTS);
### mudar forma de calculo dos valores, puxar dados do pais e correspondente e caixa
$GETMAddress = $GLOBALS['MYT']->db_array("SELECT OriNombre, smb_CttEmail, ref, CorrNombre, pai_NombreUSD, cvt_Name, username FROM systemoffice.main_users 
				INNER JOIN systemoffice.staff_members ON smb_ID = StaffMember
				INNER JOIN origenes ON OriNumero = {$GET[1]['tdm_Domain']}
				LEFT OUTER JOIN corresponsales ON CorrNumero = {$GET[1]['tdm_Corresponsal']}
				LEFT OUTER JOIN hd_paises ON pai_CodElite = {$GET[1]['tdm_Country']}
				LEFT OUTER JOIN hd_cashiertypes ON cvt_ID = {$GET[1]['tdm_Cashier']}
				LEFT OUTER JOIN hd_monedasiso ON miso_cod_elite = {$GET[1]['tdm_Currency']}
				WHERE ref = {$GET[1]['tdm_User']} OR RefDept = {$rp_dep}");
if ($GET[1]['tdm_Service'] == 1) {
    $_sysMailTopic = "REMESSAS | {$GETMAddress[1]['OriNombre']} - {$GET[1]['tdm_Domain']}, enviado por {$GETMAddress[1]['username']}";
} elseif ($GET[1]['tdm_Service'] == 2) {
    $_sysMailTopic = "EXCHANGE | {$GETMAddress[1]['OriNombre']} - {$GET[1]['tdm_Domain']}, enviado por {$GETMAddress[1]['username']}";
}

$HTML = $GET[1]['tdm_Description'] . "<br />
		----------------------------------------------------------------------------------------<br />";
if ($GET[1]['tdm_Service'] == 1) {
    if ($GET[1]['tdm_Corresponsal'] > 0) {
        $HTML .= "Correspondente: {$GETMAddress[1]['CorrNombre']}";
    } elseif ($GET[1]['tdm_Country'] > 0) {
        $HTML .= "País: {$GETMAddress[1]['pai_NombreUSD']}";
    } elseif ($GET[1]['tdm_Cashier'] > 0) {
        $HTML .= "Caixa: {$GETMAddress[1]['cvt_Name']}";
    }
} elseif ($GET[1]['tdm_Service'] == 2) {
    $HTML .= "Moeda: {$GETMAddress[1]['CorrNombre']}";
}
$HTML .= "<br />Valor Total em Euros: {$GET[1]['tdm_TFraSent']}<br />
		Valor Total no Destino: {$GET[1]['tdm_TFraPay']}<br />
		Total de Opera��es: {$GET[1]['tdm_InvTotal']}<br />
		Primeira e �ltima remessa: {$GET[1]['tdm_StartEnd']}<br />
		Data dos valores: {$GET[1]['tdm_DateValue']}<br />
		Data do Envio: {$sysTime}";

$_sysSenderName = $GET[1]['tdm_User'];
$ATTCH = $SheetPad . $File;
if (file_exists($ATTCH)) {
    for ($r = 1; $r <= $GETMAddress[0]; $r++) {
        authmail('<no-reply@moneyexpress.pt>', 'ASD', $GETMAddress[$r]['smb_CttEmail'], 'Lote Loja - ' . $_sysSenderName, $_sysMailTopic, '', $HTML, $ATTCH, $_sysconfMailString);
    }
} elseif ($GET[1]['tdm_Service'] == 1) {
    $PrcResult == 0;
}
echo 'ok';

